package com.loong.android.tools;

import java.lang.reflect.Array;

/**
 * 数组操作工具
 */
public class ArrayUtils {

    /**
     * 取数组长度
     * @param array 操作的数组
     */
    public static <T> int length(T array) {
        if (array == null || !array.getClass().isArray()) return 0;
        return Array.getLength(array);
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static <T> T[] add(T[] array, T data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        T[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static boolean[] add(boolean[] array, boolean data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        boolean[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static char[] add(char[] array, char data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        char[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static byte[] add(byte[] array, byte data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        byte[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static short[] add(short[] array, short data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        short[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static int[] add(int[] array, int data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        int[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static long[] add(long[] array, long data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        long[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static float[] add(float[] array, float data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        float[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 添加数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param data 添加的成员数据
     */
    public static double[] add(double[] array, double data) {
        if (array == null || !array.getClass().isArray()) return null;
        int totalLength = Array.getLength(array);
        double[] result = java.util.Arrays.copyOf(array, totalLength + 1);
        result[totalLength] = data;
        return result;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static <T> T[] add(T[] array, int index, T data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        T[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static boolean[] add(boolean[] array, int index, boolean data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        boolean[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static byte[] add(byte[] array, int index, byte data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        byte[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static char[] add(char[] array, int index, char data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        char[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static short[] add(short[] array, int index, short data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        short[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static int[] add(int[] array, int index, int data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        int[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static long[] add(long[] array, int index, long data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        long[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static float[] add(float[] array, int index, float data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        float[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 插入成员数据，返回新的数组对象
     * @param array 操作的数组
     * @param index 插入的位置
     * @param data 插入的成员数据
     */
    public static double[] add(double[] array, int index, double data) {
        if (array == null) return null;
        if (index < 0) index = 0;
        int totalLength = array.length;
        if (index > totalLength) index = totalLength;
        double[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[0] = data;
            result2 = java.util.Arrays.copyOf(array, totalLength);
            System.arraycopy(result2, 0, result1, 1, result2.length);
        } else if (index == totalLength) {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[totalLength] = data;
        } else {
            result1 = java.util.Arrays.copyOf(array, totalLength + 1);
            result1[index] = data;
            result2 = java.util.Arrays.copyOfRange(array, index, totalLength);
            System.arraycopy(result2, 0, result1, index + 1, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static <T> T[] remove(T[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        T[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static boolean[] remove(boolean[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        boolean[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static byte[] remove(byte[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        byte[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static char[] remove(char[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        char[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static short[] remove(short[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        short[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static int[] remove(int[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        int[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static long[] remove(long[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        long[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static float[] remove(float[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        float[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 删除数组成员，返回新的数组对象
     * @param array 操作的数组
     * @param index 删除的成员位置
     */
    public static double[] remove(double[] array, int index) {
        if (array == null || array.length <= 0) return array;
        if (index < 0 || index > array.length - 1) return array;
        double[] result1, result2;
        if (index == 0) {
            result1 = java.util.Arrays.copyOfRange(array, 1, array.length);
        } else if (index == array.length - 1) {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
        } else {
            result1 = java.util.Arrays.copyOf(array, array.length - 1);
            result2 = java.util.Arrays.copyOfRange(array, index + 1, array.length);
            System.arraycopy(result2, 0, result1, index, result2.length);
        }
        return result1;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static <T> T[] merge(T[] array, T[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (T[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        T[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (T[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static boolean[] merge(boolean[] array, boolean[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (boolean[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        boolean[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (boolean[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static byte[] merge(byte[] array, byte[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (byte[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        byte[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (byte[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static char[] merge(char[] array, char[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (char[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        char[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (char[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static short[] merge(short[] array, short[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (short[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        short[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (short[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static int[] merge(int[] array, int[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (int[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        int[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (int[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static long[] merge(long[] array, long[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (long[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        long[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (long[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static float[] merge(float[] array, float[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (float[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        float[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (float[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 合并数组，返回新的数组对象
     * @param array 操作的数组
     * @param otherArrays 需要添加的数组，可传入多个
     */
    public static double[] merge(double[] array, double[]... otherArrays) {
        if (array == null) return null;
        int totalLength = array.length;
        for (double[] arr : otherArrays) {
            if (arr != null) totalLength += arr.length;
        }
        double[] result = java.util.Arrays.copyOf(array, totalLength);
        int offset = array.length;
        for (double[] arr : otherArrays) {
            if (arr != null) {
                System.arraycopy(arr, 0, result, offset, arr.length);
                offset += arr.length;
            }
        }
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static <T> T[] copy(T[] array, int startIndex, int copySize, T[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        T[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static boolean[] copy(boolean[] array, int startIndex, int copySize, boolean[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        boolean[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static byte[] copy(byte[] array, int startIndex, int copySize, byte[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        byte[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static char[] copy(char[] array, int startIndex, int copySize, char[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        char[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static short[] copy(short[] array, int startIndex, int copySize, short[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        short[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static int[] copy(int[] array, int startIndex, int copySize, int[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        int[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static long[] copy(long[] array, int startIndex, int copySize, long[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        long[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static float[] copy(float[] array, int startIndex, int copySize, float[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        float[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     * @param targetArray 复制到的数组
     * @param pasteIndex 粘贴的位置
     */
    public static double[] copy(double[] array, int startIndex, int copySize, double[] targetArray, int pasteIndex) {
        if (targetArray == null || array == null) return null;
        if (startIndex < 0) startIndex = 0;
        if (pasteIndex < 0) pasteIndex = 0;
        int len = array.length;
        if (startIndex >= len) return targetArray;
        if (startIndex + copySize > len) copySize = len - startIndex;
        len = targetArray.length;
        if (pasteIndex > len) pasteIndex = len;
        double[] result;
        if (pasteIndex + copySize > len) result = java.util.Arrays.copyOf(targetArray, pasteIndex + copySize);
        else result = java.util.Arrays.copyOf(targetArray, len);
        System.arraycopy(array, startIndex, result, pasteIndex, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static <T> T[] copy(T[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        T[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static boolean[] copy(boolean[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        boolean[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static byte[] copy(byte[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        byte[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static char[] copy(char[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        char[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static short[] copy(short[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        short[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static int[] copy(int[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        int[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static long[] copy(long[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        long[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static float[] copy(float[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        float[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 复制数组，返回新的数组对象
     * @param array 用作复制的数组
     * @param startIndex 起始位置
     * @param copySize 复制成员数
     */
    public static double[] copy(double[] array, int startIndex, int copySize) {
        if (startIndex < 0) startIndex = 0;
        if (array == null || array.length <= 0 || startIndex >= array.length || copySize <= 0) return array;
        if (startIndex + copySize > array.length) copySize = array.length - startIndex;
        double[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, startIndex, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static <T> T[] copyLeft(T[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        T[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static boolean[] copyLeft(boolean[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        boolean[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static byte[] copyLeft(byte[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        byte[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static char[] copyLeft(char[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        char[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static short[] copyLeft(short[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        short[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static int[] copyLeft(int[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        int[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static long[] copyLeft(long[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        long[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static float[] copyLeft(float[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        float[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从左边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static double[] copyLeft(double[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        double[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, 0, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static <T> T[] copyRight(T[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        T[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static boolean[] copyRight(boolean[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        boolean[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static byte[] copyRight(byte[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        byte[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static char[] copyRight(char[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        char[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static short[] copyRight(short[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        short[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static int[] copyRight(int[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        int[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static long[] copyRight(long[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        long[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static float[] copyRight(float[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        float[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

    /**
     * 从右边复制数组，返回新的数组实例
     * @param array 用作复制的数组
     * @param copySize 复制的成员数
     */
    public static double[] copyRight(double[] array, int copySize) {
        if (array == null || array.length <= 0 || copySize <= 0) return array;
        if (copySize > array.length) copySize = array.length;
        double[] result = java.util.Arrays.copyOf(array, copySize);
        System.arraycopy(array, array.length - copySize, result, 0, copySize);
        return result;
    }

}
